﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Sudoku.Lang;
using Services;
using Microsoft.Xna.Framework.Graphics;
using Sudoku.Utilities;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Phone.Tasks;

namespace Sudoku.Menu
{
	public class MainScene : ExtendedGameComponent
	{

		Texture2D bg;

		public MainScene(Game game)
			: base(game)
		{

		}

		public override void Initialize()
		{
			SpriteFont font = fontService.Font(FontType.Delicious26Bold);

			Rectangle menuPos;
			Rectangle howPos;
			Rectangle quitPos;

			menuPos = new Rectangle(40, 90, 150, 85);
			howPos = new Rectangle(40, 175, 150, 85);
			quitPos = new Rectangle(40, 260, 150, 85);

			LabelButton menuPlayBtn = new LabelButton(Game, menuPos, LocalizedStrings.MenuPlay, font, Color.White);
			menuPlayBtn.Texture = textureService.Texture("Button1");
			menuPlayBtn.OnTap += (Button sender) => { StateManagement.StateManager.DifficultyScreen(Game); };

			Components.Add(menuPlayBtn);

			LabelButton menuHowBtn = new LabelButton(Game, howPos, LocalizedStrings.MenuHowto, font, Color.White);
			menuHowBtn.Texture = textureService.Texture("Button1");
			menuHowBtn.OnTap += (Button sender) => { StateManagement.StateManager.HowtoScreen(Game); };
			Components.Add(menuHowBtn);

			LabelButton menuQuitBtn = new LabelButton(Game, quitPos, LocalizedStrings.MenuQuit, font, Color.White);
			menuQuitBtn.Texture = textureService.Texture("Button1");
			menuQuitBtn.OnTap += (Button sender) => { Game.Exit(); };
			Components.Add(menuQuitBtn);


			ISaveService saveService = (ISaveService)Game.Services.GetService(typeof(ISaveService));
			 
			if (saveService.IsSaveAvailable())
			{
				LabelButton continueBtn = new LabelButton(Game, new Rectangle(40, 365, 150, 85), LocalizedStrings.MenuContinue, font, Color.White);
				continueBtn.Texture = textureService.Texture("Button1");
				continueBtn.OnTap += (Button sender) => { StateManagement.StateManager.ContinueGame(Game, saveService.LoadGame()); };
				continueBtn.Enabled = saveService.IsSaveAvailable();
				Components.Add(continueBtn);
			}
			
			IScoreService scoreService = Game.Services.GetService(typeof(IScoreService)) as IScoreService;
			HighScore score = scoreService.RetrieveScore();

			BannerComponent scoreBanner = new BannerComponent(Game);
			scoreBanner.Color = new Color(0, 102, 153);
			scoreBanner.Font = fontService.Font(FontType.DeliciousSmallCaps28);

			string s;
			
			if(Guide.IsTrialMode)
			{
				ITrialService trial = (ITrialService)Game.Services.GetService(typeof(ITrialService));
				short i = trial.GamesLeft();
				if(i <= 0)
				{
					s = "   ** Sudoku " + LocalizedStrings.TrialNoGames + " **   ";
					menuPlayBtn.Enabled = false;
				}
				else s = String.Format("   ** Sudoku " + LocalizedStrings.TrialNGames + " **   ", i, (i > 1) ? "s" : "");
			}
			else
			{
				s = " - " + LocalizedStrings.MenuScores + " - ";
				s += LocalizedStrings.MenuBeginner + ": " + ((score.Beginner.Time == 0) ? "-" : Utils.GetTimeString(score.Beginner.Time)) + " " + score.Beginner.Name + " ** ";
				s += LocalizedStrings.MenuNormal + ": " + ((score.Normal.Time == 0) ? "-" :  Utils.GetTimeString(score.Normal.Time)) + " " + score.Normal.Name + " ** ";
				s += LocalizedStrings.MenuExperienced + ": " + ((score.Expert.Time == 0) ? "-" :  Utils.GetTimeString(score.Expert.Time)) + " " + score.Expert.Name + " ** ";
				s += LocalizedStrings.MenuExtreme + ": " + ((score.Extreme.Time == 0) ? "-" :  Utils.GetTimeString(score.Extreme.Time)) + " " + score.Extreme.Name + " ** ";
			}

			scoreBanner.Message = s;
			scoreBanner.Position = new Vector2(0, 22);
			scoreBanner.NumberOfChars = 60;
			scoreBanner.ScrollSpeed = 200;
			Components.Add(scoreBanner);

			LabelButton creditsButton = new LabelButton(Game, new Rectangle(188, 710, 104, 85), LocalizedStrings.Credits, fontService.Font(FontType.Delicious13), Color.Black, LabelStyle.BOTTOM);
			creditsButton.Texture = textureService.Texture("Credits");
			creditsButton.OnTap += (Button sender) => { StateManagement.StateManager.CreditsScreen(Game); };
			Components.Add(creditsButton);

			bg = textureService.Background("Main");
			var inputService = Game.Services.GetService(typeof(IInputManager)) as IInputManager;
			inputService.OnBack += new InputManager.PhoneButtonEvent(Back);

			base.Initialize();
		}

#if !SUDOKU_MASTER
		private void OnAdsTap(Button sender)
		{
			// Show an application, using the default ContentType.
			MarketplaceDetailTask marketplaceDetailTask = new MarketplaceDetailTask();
			marketplaceDetailTask.ContentIdentifier = "7a1eb755-d8da-df11-a844-00237de2db9e";
			marketplaceDetailTask.ContentType = MarketplaceContentType.Applications;
			marketplaceDetailTask.Show();
		}	
#endif

		private void Back()
		{
			Game.Exit();
		}

		public override void Draw(GameTime gameTime)
		{
			spriteBatch.Begin();

			spriteBatch.Draw(bg, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.White);

			spriteBatch.End();

			base.Draw(gameTime);
		}
	}
}
